package com.kcs.fxmargin.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.action.form.ImportRRAndMMActionForm;
import com.kcs.fxmargin.bo.ImportRRAndMMBO;
import com.kcs.fxmargin.service.ImportRRAndMMService;

public class ImportRRAndMMAction extends FXAction {

	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ImportRRAndMMActionForm bean = null;
		ImportRRAndMMBO bo = null;
		ImportRRAndMMService svc = null;
		
		log.info("Begin : ImportRRAndMMAction : process");
		
		try{
			bean = (ImportRRAndMMActionForm)form;
			
			bo = new ImportRRAndMMBO();
			svc = new ImportRRAndMMService();
			
			log.info("tranCode="+bean.getTranCode());
			
			if(bean.getTranCode().equals("")){
				bean.resetForm();
				bean.setInAsOfDate(commonUtils.getcurrentDatetime().substring(0,10));
				
			}else if(bean.getTranCode().equals("searchImportRRAndMMList")){
				if(validateDateFormat(bean.getInAsOfDate(),request)){
					bo.setInAsOfDate(formatAsOfDate(bean.getInAsOfDate()));
				}
				
				bo = svc.getImportRRAndMMList(bo);
				bean.setImportRRAndMMList(processImportRRAndMMList(bo.getImportRrAndMmList()));
				request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
			}else if(bean.getTranCode().equals("resetForm")){
				bean.resetForm();
				bean.setInAsOfDate(commonUtils.getcurrentDatetime().substring(0,10));
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute(Constant.ERR_ATTR, e+"");
			
		}finally{
			if(bean != null){
				bean.setTranCode("");
			}
		}

		log.info("End : ImportRRAndMMAction : process");
		return mapping.findForward("importRRAndMM");
	}
	
	private boolean validateDateFormat(String date, HttpServletRequest request)throws Exception{
		boolean result = false;
		if (!date.equals("")){
			if(commonUtils.checkDateFormat(date)){
				result = true;
			}else{
				request.setAttribute(Constant.ERR_ATTR, Constant.ERR_DATE_FORMAT);
				result = false;
			}
			
		}else{
			result = true;
		}
		return result;
	}
	
	private String formatAsOfDate(String date)throws Exception{
		String[] spl = null;
		String d = null;
		String m = null;
		String y = null;
		String result = date;
		
		if(date != null && !date.equals("")){
			spl = date.split("/");
			d = spl[0];
			m = spl[1];
			y = spl[2];
			
			result = y+"-"+m+"-"+d;
		}
		
		return result;
	}
	
	private List<ImportRRAndMMActionForm> processImportRRAndMMList(List<ImportRRAndMMBO> boList)throws Exception{
		List<ImportRRAndMMActionForm> resultList = null;
		List<String> uniqueTime = null;
		List<String> uniqueCurrency = null;
		ImportRRAndMMActionForm form = null;
		String t = null;
		String c = null;
		int rowNum = 0;
		
		log.info("Begin : ImportRRAndMMAction : processImportRRAndMMList");
		
		if(boList != null){
			resultList = new ArrayList<ImportRRAndMMActionForm>();
			uniqueTime = getUniqueTimeFromList(boList);
			uniqueCurrency = getUniqueCurrencyFromList(boList);
			
			for(int i=0; i<uniqueTime.size(); i++){
				for(int j=0; j<uniqueCurrency.size(); j++){
					form = new ImportRRAndMMActionForm();
					t = uniqueTime.get(i);
					c = uniqueCurrency.get(j);
					
					form.setTime(t);
					form.setCurrency(c);
					form.setToday(getDataFromList(boList,t,c,"TODAY"));
					form.setTom(getDataFromList(boList,t,c,"TOM"));
					form.setSpot(getDataFromList(boList,t,c,"SPOT"));
					form.set_1w(getDataFromList(boList,t,c,"1W"));
					form.set_2w(getDataFromList(boList,t,c,"2W"));
					form.set_1m(getDataFromList(boList,t,c,"1M"));
					form.set_2m(getDataFromList(boList,t,c,"2M"));
					form.set_3m(getDataFromList(boList,t,c,"3M"));
					form.set_6m(getDataFromList(boList,t,c,"6M"));
					form.set_9m(getDataFromList(boList,t,c,"9M"));
					form.set_1y(getDataFromList(boList,t,c,"1Y"));
					form.set_2y(getDataFromList(boList,t,c,"2Y"));
					form.setRowNum(++rowNum);
					
					resultList.add(form);
				}
			}
		}
		
		log.info("import RR And MM size = "+((resultList==null)?"null":resultList.size()));
		
		log.info("End : ImportRRAndMMAction : processImportRRAndMMList");
		return resultList;
	}
	
	private List<String> getUniqueTimeFromList(List<ImportRRAndMMBO> boList)throws Exception{
		List<String> resultList = null;
		HashSet<String> hs = null;
		ImportRRAndMMBO bo = null;
		
		if(boList != null){
			resultList = new ArrayList<String>();
			hs = new HashSet<String>();
			
			for(int i=0; i<boList.size(); i++){
				bo = boList.get(i);
				hs.add(bo.getTime());
			}
			
			for(String s : hs){
				resultList.add(s);
			}
			
		}
		
		return resultList;
	}
	
	private List<String> getUniqueCurrencyFromList(List<ImportRRAndMMBO> boList)throws Exception{
		List<String> resultList = null;
		HashSet<String> hs = null;
		ImportRRAndMMBO bo = null;
		
		if(boList != null){
			resultList = new ArrayList<String>();
			hs = new HashSet<String>();
			
			for(int i=0; i<boList.size(); i++){
				bo = boList.get(i);
				hs.add(bo.getCurr());
			}
			
			for(String s : hs){
				resultList.add(s);
			}
			
			Collections.sort(resultList);
			
		}
		
		return resultList;
	}
	
	private String getDataFromList(List<ImportRRAndMMBO> boList, String time, String currency, String period)throws Exception{
		ImportRRAndMMBO bo = null;
		
		if(boList != null){
			for(int i=0; i<boList.size(); i++){
				bo = boList.get(i);
				if(bo.getTime().equals(time) && bo.getCurr().equals(currency) && bo.getPeriod().equals(period)){
					return bo.getRate();
				}
			}
		}
		
		return null;
	}

}
